﻿namespace ETestIT.UI
{
    using System;
    using System.Web.UI.WebControls;
    using Core;
    using Core.Storage;
    using Domain.Entities;
    using Domain.Repositories;
    using Presentation;
    using StructureMap;

    public partial class Candidats : PageBase<Candidats>
    {
        private ICadidateRepository candidateRepository;
        private ISessionStorage sessionStorage;

        private const string Editcandidatekey = "editCandidateKey";

        protected void Page_Load(object sender, EventArgs e)
        {
            candidateAddEdit.Save += CandidateAddEditSave;

            candidateRepository = ObjectFactory.GetInstance<ICadidateRepository>();
            sessionStorage = ObjectFactory.GetInstance<ISessionStorage>();

            if (!IsPostBack)
                LoadAllCandidats();
        }

        private void LoadAllCandidats()
        {
            gvCandidats.DataSource = candidateRepository.GetAll();
            gvCandidats.DataBind();
        }

        protected void BtnAddNewCandidate_Clicked(object sender, EventArgs e)
        {
            candidateAddEdit.Visible = true;
        }

        protected void LnkEditCommand(object sender, CommandEventArgs e)
        {
            var candId = new Guid(e.CommandArgument.ToString());

            switch (e.CommandName)
            {
                case "CandidatsEdit":
                    {
                        candidateAddEdit.SetModel(candidateRepository.GetById(candId));
                        sessionStorage.Set(Editcandidatekey, candId);
                        candidateAddEdit.Visible = true;
                    }
                    break;
                case "CandidatsDelete":
                    {
                        candidateRepository.Delete(candId);
                        LoadAllCandidats();
                    }
                    break;
                case "ViewCandidateResume":
                    {
                        sessionStorage.Set(Const.SelectedCandidateKey, candId);
                        Response.Redirect("~/Resumes.aspx");
                    }
                    break;
            }
        }

        private void CandidateAddEditSave(object sender, EventArgs e)
        {
            Candidate theCandidate;
            Action<Candidate> theActionOnCandidate;

            if (sessionStorage.Contains(Editcandidatekey))
            {
                theCandidate = candidateRepository.GetById(sessionStorage.Get<Guid>(Editcandidatekey));
                theActionOnCandidate = u => candidateRepository.Update(u);
            }
            else
            {
                theCandidate = new Candidate();
                theActionOnCandidate = u => candidateRepository.Add(u);
            }

            candidateAddEdit.UpdateModel(theCandidate);
            theActionOnCandidate(theCandidate);
            LoadAllCandidats();

            if(sessionStorage.Contains(Editcandidatekey))
                sessionStorage.Remove(Editcandidatekey);

            candidateAddEdit.Visible = false;
        }
    }
}